<?php
/*
 * @Description    : Lib
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 2022-03-25 14:04:16
 * @LastEditors    : QianLong
 * @LastEditTime   : 2022-03-25 14:05:56
 */

namespace Ecmpay\Ecmpay;

class Lib
{
    /**
     * 网络请求
     * @param string $url
     * @param array $data
     * @param integer $timeout
     * @param boolean $https
     * @param array $header
     * @return array
     * @author QianLong <87498106@qq.com>
     * @version 1.0.0
     * @date 2019-09-21 10:07:47
     */
    public static function httpRequest($url, $data = '', $timeout = 15, $https = false, $header = [])
    {
        $results['error']   = '';
        $results['status']  = 0;
        $results['data']    = array();
        $curl               = curl_init();                              // 启动一个CURL会话
        if (is_array($data) || is_array(json_decode($data, true))) {
            curl_setopt($curl, CURLOPT_POST, 1);
            if (!empty($data)) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
        }
        if (!empty($header) && is_array($header)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);              // Post提交的数据包
        }
        if ($https) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);              // 对认证证书来源的检查
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);              // 从证书中检查SSL加密算法是否存在
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);              // 使用自动跳转
        }
        curl_setopt($curl, CURLOPT_URL, $url);                      // 要访问的地址
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);                    // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0);                      // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);              // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);                 // 自动设置Referer

        $results['data'] = curl_exec($curl);                     // 执行操作
        if (curl_errno($curl)) {
            $results['error']    = curl_error($curl);                    //捕抓异常
        }
        curl_close($curl);                                              // 关闭CURL会话
        return $results;
    }
    /**
     * 生成签名
     * @param [type] $param
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2022-03-11 17:21:15
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2022-03-11 17:21:15
     */
    public static function createSign($param, $secret)
    {
        if (is_array($param)) {
            unset($param['sign']);
            foreach ($param as $k => $v) {
                if (!empty($v)) {
                    $Parameters[$k] = $v;
                }
            }
            unset($param, $k, $v);
            // 签名步骤一：按字典序排序参数
            ksort($Parameters);
            $buff = '';
            foreach ($Parameters as $k => $v) {
                $buff .= $k . $v;
            }
            unset($Parameters, $k, $v);
            $buff = $secret . $buff . $secret;
            $sign = strtoupper(md5($buff));
            unset($buff, $appInfo);
            return $sign;
        }
    }
}
